package com.andy.qhb.game;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.entity.ConfCommon;
import com.andy.qhb.utils.GoConstant;
import com.andy.qhb.utils.IdGen;
import com.google.common.collect.Maps;

public class GameConf {

	
	private static Map<String,ConfCommon> confCommons;

	
	public static void init() {
	
		initCommons();
		
	}

	public static void initCommons() {
		Map<String,ConfCommon> confCommonsT = Maps.newHashMap();
		List<ConfCommon> commons = DaoUtils.getList(ConfCommon.class);
		for(ConfCommon comm:commons){
			confCommonsT.put(comm.getNid(), comm);
		}
		confCommons = confCommonsT;
		initConf();
	}

	public static String[] firstNames;
	public static String[] secondNames;
	public static int[] leiReward;
	public static int[] qbReward;
	public static int[] fbReward71;
	public static int[] jqFbReward;
	public static int[] fuliFabCoin;
	
	public static int[] roomCoinGroup1;
	public static int[][] roomCoinGroup2;
	public static int groupCreateLimit;
	public static String heepay_key = "E1EDAB77A6B6412E8F8B19D4";//商户密钥
	public static String agent_id = "2113420";//商户号
	public static IdGen idGen;
	public static void initConf() {
		if(Global.sysCurrHave == null){
			Global.sysCurrHave = new AtomicLong(getConfInt("sys_curr_have"));
		}
		
		firstNames = getConfStr("first_name").split(",");
		secondNames = getConfStr("second_name").split(",");
		groupCreateLimit = getConfInt("group_create_limit");
		
		leiReward = getConfInts("lei_reward");
		qbReward = getConfInts("qb_reward");
		fbReward71 = getConfInts("fb_reward_7_1");
		jqFbReward = getConfInts("jq_fb_reward");
		fuliFabCoin = getConfInts("fuli_fab_coin");
		
		roomCoinGroup1 = getConfInts("room_coin_group_1");
		String[] g2 = getConfStr("room_coin_group_2").split(";");
		roomCoinGroup2 = new int[g2.length][4];
		for(int i=0;i<g2.length;i++) {
			String[] g2n = g2[i].split(",");
			for(int j=0;j<g2n.length;j++) {
				roomCoinGroup2[i][j] = Integer.parseInt(g2n[j]);
			}
		}
		
		
		if(idGen == null){
			idGen = new IdGen("id_gen");
		}
		
		// 初始化路人房间
		if(Global.getNorRooms().isEmpty()) {
			Global.initNorRooms(GoConstant.GAME_TYPE_S);
			Global.initNorRooms(GoConstant.GAME_TYPE_J);
			Global.initNorRooms(GoConstant.GAME_TYPE_F);
		}
		
	}

	public static ConfCommon getConf(String nid){
		return confCommons.get(nid);
	}
	
	public static int getConfInt(String nid){
		String v = getConfStr(nid);
		if(v != null){
			return Integer.parseInt(v);
		}
		return 0;
	}
	public static int[] getConfInts(String nid){
		String v = getConfStr(nid);
		if(v != null){
			String[] strs = v.split(",");
			int[] result = new int[strs.length];
			for(int i=0;i<strs.length;i++){
				result[i] = Integer.parseInt(strs[i]);
			}
			return result;
		}
		return null;
	}
	public static String getConfStr(String nid){
		ConfCommon common = confCommons.get(nid);
		if(common != null){
			return common.getValue();
		}
		return "";
	}
	
	public static int genId(){
		if(idGen != null){
			return idGen.genId();
		}
		return 0;
	}

}
